package com.transcargo.util;

import java.util.StringTokenizer;
import java.util.Date;

public class Normalizer {
	public static String NormalizeDate(String date) {
		String result = "";
		StringTokenizer tok = new StringTokenizer(date,".");
		if (tok.countTokens() < 3) return "";
		String day,month,year;
		day = tok.nextToken();
		month = tok.nextToken();
		year = tok.nextToken();
		if (Integer.parseInt(year) < 100) year = Integer.toString(2000+Integer.parseInt(year));
		result = year + "-" + month + "-" + day;
		return result;
	}

	public static String DenormalizeDate(String date) {
		String result = "";
		StringTokenizer tok = new StringTokenizer(date,"-");
		if (tok.countTokens() < 3) return "";
		String day,month,year;
		year = tok.nextToken();
		month = tok.nextToken();
		day = tok.nextToken();
		result = day + "." + month;
		return result;
	}
	public static String DenormalizeDateFull(String date) {
		String result = "";
		StringTokenizer tok = new StringTokenizer(date,"-");
		if (tok.countTokens() < 3) return "";
		String day,month,year;
		year = tok.nextToken();
		month = tok.nextToken();
		day = tok.nextToken();
		result = day + "." + month + "." + year;
		return result;
	}
	
	public static String DenormalizeTime(String time) {
		String result = "";
		StringTokenizer tok = new StringTokenizer(time,"- :.");
		if (tok.countTokens() < 6) return "";
		int day,month,year, hour, min, sec;
		year = Integer.parseInt(tok.nextToken());
		month = Integer.parseInt(tok.nextToken());
		day = Integer.parseInt(tok.nextToken());
		hour = Integer.parseInt(tok.nextToken());
		min = Integer.parseInt(tok.nextToken());
		sec = Integer.parseInt(tok.nextToken());
		Date today = new Date();
		Date yesterday = new Date(today.getTime() - 86400000);
		if (today.getDate() == day && today.getMonth() + 1 == month && today.getYear() + 1900 == year) result = "Сегодня, в ";
		else if (yesterday.getDate() == day && yesterday.getMonth() + 1 == month && yesterday.getYear() + 1900 == year) result = "Вчера, в ";
		else if (today.getYear() != year) result = ((day<10)?"0":"") + day + "." + ((month<10)?"0":"") + "." + year + " в "; 
		else result = ((day<10)?"0":"") + day + "." + ((month<10)?"0":"") + month + " в ";
		result = result + hour + ":" + (min<10?"0":"") + min;
		return result;
	}
}
